package com.bjb.vr.manager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.SysUpgradeDevAo;
import com.bjb.vr.common.ao.SysUpgradeDevLogAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.SysUpgradeDevLog;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.SysUpgradeDevLogService;
import com.bjb.vr.common.service.SysUpgradeService;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.DevUpgradeStatisticsVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import com.bjb.vr.common.vo.UpgradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/8/11
 */
@Slf4j
@RestController
@RequestMapping("/api/manager/v1")
public class DevUpgradeController {


	@Autowired
	SysUpgradeDevLogService sysUpgradeDevLogService;

	@Autowired
	SysUpgradeService sysUpgradeService;

	@Autowired
	RedisUtil redisUtil;

	/**
	 * 获取最近设备更新统计信息
	 * @return
	 */
	@PostMapping("/queryDevSysUpgradeStatisticsInfo")
	public BaseResult<DevUpgradeStatisticsVo> queryDevSysUpgradeStatisticsInfo(){
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		DevUpgradeStatisticsVo  devUpgradeStatisticsVo = sysUpgradeDevLogService.queryDevSysUpgradeSumInfo(umsAccountVo.getTenantId());
		if(devUpgradeStatisticsVo!=null){
			devUpgradeStatisticsVo.setIncompleteCount(devUpgradeStatisticsVo.getTotalCount()-devUpgradeStatisticsVo.getSuccessCount());
		}
		return CommonResult.Ok().setResult(devUpgradeStatisticsVo);
	}

	/**
	 * 获取设备升级管理查询列表
	 * @param sysUpgradeDevLogAo
	 * @return
	 */
	@PostMapping("/queryDevSysUpgradeLogPage")
	public BaseResult<IPage<SysUpgradeDevLog>> queryDevSysUpgradeLogPage(@RequestBody SysUpgradeDevLogAo sysUpgradeDevLogAo){
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		IPage<SysUpgradeDevLog> sysUpgradeDevLogIPage = sysUpgradeDevLogService.queryDevSysUpgradeLogPage(umsAccountVo.getTenantId(),sysUpgradeDevLogAo);
		return CommonResult.Ok().setResult(sysUpgradeDevLogIPage);
	}

	@PostMapping("/queryDevLatestVersion")
	public BaseResult<UpgradeVo> queryDevLatestVersion() {
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		UpgradeVo upgradeVo = sysUpgradeService.queryDevLatestVersion(umsAccountVo.getTenantId());
		return CommonResult.Ok().setResult(upgradeVo);
	}

	@PostMapping("/queryApkUpgradeProgress")
	public BaseResult<Long> queryApkUpgradeProgress(@RequestBody SysUpgradeDevAo sysUpgradeDevAo) {
		String APK_UPGRADE_DOWNLOAD_PROGRESS_KEYS = "apk_upgrade_download_progress_keys:";
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		Object oprogress = redisUtil.get(APK_UPGRADE_DOWNLOAD_PROGRESS_KEYS + umsAccountVo.getTenantId() + "-" + sysUpgradeDevAo.getSysUpdateId());
		Long progress = null;
		if(oprogress!=null) {
			 progress = Long.valueOf(oprogress.toString());
		}else{
			return CommonResult.Ok().setResult(-1);
		}
		return CommonResult.Ok().setResult(progress);
	}
}
